package orest.back.market;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import orest.back.userneeds.UserNeedsHandler;
import orest.front.achieve.AchieveHandler;




import net.ssta.common.Constants;
import net.ssta.common.LoginUser;
import net.ssta.core.base.ServletBase;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.RecordSet;
import net.ssta.core.db.Record;
import net.ssta.core.util.DateUtility;
import net.ssta.core.web.RequestKit;

public class MarketServlet extends ServletBase {

	protected void defaultAction(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
			System.out.println("MarketServlet default Action");
	}
	
	/**
     * 获取用户技术需求列表
     * 
     * @param request
     * @param response
     */
    public void techneedList(HttpServletRequest request,
        HttpServletResponse response)
    {
    	try {
			try {
				
				RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
		        PagingInfo pagingInfo = new PagingInfo(request, 10, "^(?!.*(result)|(LTPAToken)).*$");
		        Record searchRcd = requestKit.getRecordWithPrefix("");
		        
		        MarketHandler markethandler = new MarketHandler();
		        RecordSet techneedSet = markethandler.getTechneedList(requestKit, pagingInfo);
		        
		        Map fildOptionMap = new HashMap();
		        RecordSet newEnerClass = AchieveHandler.getNewEnergyClassList(); 
		        	//markethandler.getFildOption("common","NewEnergyClass");
		        
		        for(int i=0;i<newEnerClass.size();i++){
		        	Record rec = newEnerClass.get(i);
		        	fildOptionMap.put("NEWENERGYCLASS"+rec.getString("OPTIONVALUE","").trim(),rec.getString("OPTIONNAME",""));
		        }
		        
		        request.setAttribute("pagingInfo", pagingInfo);
		        request.setAttribute("techneedSet", techneedSet);
		        request.setAttribute("newEnerClass", newEnerClass);
		        request.setAttribute("fildOptionMap", fildOptionMap);
		        
		        request.setAttribute("searchRcd", searchRcd);
				
				forward("/back/market/techneedList.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ServletException e) {
			e.printStackTrace();
		}
    }
    
    /**
     * 设置技术需求的审批状态
     * 
     * @param request
     * @param response
     */
    public void techneedCheckStatus(HttpServletRequest request,
        HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        String uuids = requestKit.getParameter("uuids", "");
        String ifcheck = requestKit.getParameter("ifcheck", "0");
        
        String uuid[] = uuids.split(",");
        if(uuid!=null && uuid.length>0){
        	for(int i=0;i<uuid.length;i++){
        		//逐一对审批的信息进行审批状态的改变
        		markethandler.techneedCheckStatus(uuid[i], ifcheck);
        	}
        }
    }
    
    /**
     * 设置技术需求的推荐状态，将同时设置首页推荐和栏目推荐的状态，暂不分别设置
     * 
     * @param request
     * @param response
     */
    public void techneedRecommend(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        String uuids = requestKit.getParameter("uuids", "");
        String ifrecommend = requestKit.getParameter("recommend", "0");
        
        String uuid[] = uuids.split(",");
        if(uuid!=null && uuid.length>0){
        	for(int i=0;i<uuid.length;i++){
        		//逐一对审批的信息进行审批状态的改变
        		markethandler.techneedRecommend(uuid[i], ifrecommend);
        	}
        }
    }
    
    /**
     * 删除技术需求信息
     * 
     * @param request
     * @param response
     */
    public void techneedDelete(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        markethandler.deleteTechneed(requestKit);
    }
    
    /**
     * 根据uuid获取指定技术需求的详细信息
     * 
     * @param request
     * @param response
     */
    public void techneedShow(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        String uuid = requestKit.getParameter("uuid", "");
        
        Record techneedInfo = markethandler.getTechneedById(uuid);
        request.setAttribute("techneedInfo", techneedInfo);
        
        //RecordSet newEnerClass = markethandler.getFildOption("common","NewEnergyClass");
        RecordSet newEnerClass = AchieveHandler.getNewEnergyClassList();
        request.setAttribute("newenergyclass", newEnerClass);
        
        try{
        	forward("/back/market/techneedInfo.jsp");
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException ex){
			ex.printStackTrace();
		}
    }
    
    /**
     * 获取自荐项目列表
     * 
     * @param request
     * @param response
     */
    public void selloneselfList(HttpServletRequest request,
        HttpServletResponse response)
    {

			try {
				RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
		        PagingInfo pagingInfo = new PagingInfo(request, 10, "^(?!.*(result)|(LTPAToken)).*$");
		        Record searchRcd = requestKit.getRecordWithPrefix("");
		        
		        Map fildOptionMap = new HashMap();
		        
		        MarketHandler markethandler = new MarketHandler();
		        RecordSet selloneselfSet = markethandler.getSelloneselfList(requestKit, pagingInfo);
		        
		        //RecordSet newEnerClass = markethandler.getFildOption("common","NewEnergyClass");
		        RecordSet newEnerClass = AchieveHandler.getNewEnergyClassList();
		        for(int i=0;i<newEnerClass.size();i++){
		        	Record rec = newEnerClass.get(i);
		        	fildOptionMap.put("NEWENERGYCLASS"+rec.getString("OPTIONVALUE","").trim(),rec.getString("OPTIONNAME",""));
		        }
		        
		        request.setAttribute("pagingInfo", pagingInfo);
		        request.setAttribute("selloneselfSet", selloneselfSet);
		        request.setAttribute("newEnerClass", newEnerClass);
		        request.setAttribute("fildOptionMap", fildOptionMap);
		        
		        request.setAttribute("searchRcd", searchRcd);
				
				forward("/back/market/selloneselfList.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException e) {
				e.printStackTrace();
			}
    }
    
    
    /**
     * 设置自荐项目的审批状态
     * 
     * @param request
     * @param response
     */
    public void selloneselfCheckStatus(HttpServletRequest request, HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        String uuids = requestKit.getParameter("uuids", "");
        String ifcheck = requestKit.getParameter("ifcheck", "0");
        
        String uuid[] = uuids.split(",");
        if(uuid!=null && uuid.length>0){
        	for(int i=0;i<uuid.length;i++){
        		//逐一对审批的信息进行审批状态的改变
        		markethandler.selloneselfCheckStatus(uuid[i], ifcheck);
        	}
        }
    }
    
    /**
     * 设置自荐项目的推荐状态，将同时设置首页推荐和栏目推荐的状态，暂不分别设置
     * 
     * @param request
     * @param response
     */
    public void selloneselfRecommend(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        String uuids = requestKit.getParameter("uuids", "");
        String ifrecommend = requestKit.getParameter("recommend", "0");
        
        String uuid[] = uuids.split(",");
        if(uuid!=null && uuid.length>0){
        	for(int i=0;i<uuid.length;i++){
        		//逐一对审批的信息进行审批状态的改变
        		markethandler.selloneselfRecommend(uuid[i], ifrecommend);
        	}
        }
    }
    
    /**
     * 删除自荐项目需求信息
     * 
     * @param request
     * @param response
     */
    public void selloneselfDelete(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        
        String uuids = requestKit.getParameter("uuids", "");
        String ifrecommend = requestKit.getParameter("recommend", "0");
        
        String uuid[] = uuids.split(",");
        if(uuid!=null && uuid.length>0){
        	for(int i=0;i<uuid.length;i++){
        		//逐一对审批的信息进行审批状态的改变
        		markethandler.selloneselfDelete(uuid[i]);
        	}
        }
    }
    
    
    /**
     * 根据uuid获取指定自荐项目的详细信息
     * 
     * @param request
     * @param response
     */
    public void selloneselfShow(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        String uuid = requestKit.getParameter("uuid", "");
        
        Record selloneselfInfo = markethandler.getSelloneselfById(uuid);
        request.setAttribute("selloneselfinfo", selloneselfInfo);
        
        Map fildOptionMap = new HashMap();
        //RecordSet newEnerClass = markethandler.getFildOption("common","NewEnergyClass");
        RecordSet newEnerClass = AchieveHandler.getNewEnergyClassList();
        request.setAttribute("newenergyclass", newEnerClass);
        
        try{
        	forward("/back/market/selloneselfInfo.jsp");
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException ex){
			ex.printStackTrace();
		}
    }
    
    /**
     * 获取投融资项目列表
     * 
     * @param request
     * @param response
     */
    public void investList(HttpServletRequest request,
        HttpServletResponse response)
    {

			try {
				RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
		        PagingInfo pagingInfo = new PagingInfo(request, 10, "^(?!.*(result)|(LTPAToken)).*$");
		        Record searchRcd = requestKit.getRecordWithPrefix("");
		        
		        Map fildOptionMap = new HashMap();
		        
		        MarketHandler markethandler = new MarketHandler();
		        RecordSet investSet = markethandler.getInvestList(requestKit, pagingInfo);
		        
		        //RecordSet newEnerClass = markethandler.getFildOption("common","NewEnergyClass");
		        RecordSet newEnerClass = AchieveHandler.getNewEnergyClassList();
		        for(int i=0;i<newEnerClass.size();i++){
		        	Record rec = newEnerClass.get(i);
		        	fildOptionMap.put("NEWENERGYCLASS"+rec.getString("OPTIONVALUE","").trim(),rec.getString("OPTIONNAME",""));
		        }
		        
		        request.setAttribute("pagingInfo", pagingInfo);
		        request.setAttribute("investSet", investSet);
		        request.setAttribute("newEnerClass", newEnerClass);
		        request.setAttribute("fildOptionMap", fildOptionMap);
		        
		        request.setAttribute("searchRcd", searchRcd);
				
				forward("/back/market/investList.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException e) {
				e.printStackTrace();
			}
    }
    /**
     * 设置投融资项目的审批状态
     * 
     * @param request
     * @param response
     */
    public void investCheckStatus(HttpServletRequest request, HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        String uuids = requestKit.getParameter("uuids", "");
        String ifcheck = requestKit.getParameter("ifcheck", "0");
        
        String uuid[] = uuids.split(",");
        if(uuid!=null && uuid.length>0){
        	for(int i=0;i<uuid.length;i++){
        		//逐一对审批的信息进行审批状态的改变
        		markethandler.investCheckStatus(uuid[i], ifcheck);
        	}
        }
    }
    
    /**
     * 设置自荐项目的推荐状态，将同时设置首页推荐和栏目推荐的状态，暂不分别设置
     * 
     * @param request
     * @param response
     */
    public void investRecommend(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        String uuids = requestKit.getParameter("uuids", "");
        String ifrecommend = requestKit.getParameter("recommend", "0");
        
        String uuid[] = uuids.split(",");
        if(uuid!=null && uuid.length>0){
        	for(int i=0;i<uuid.length;i++){
        		//逐一对审批的信息进行审批状态的改变
        		markethandler.investRecommend(uuid[i], ifrecommend);
        	}
        }
    }
    
    /**
     * 删除自荐项目需求信息
     * 
     * @param request
     * @param response
     */
    public void investDelete(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        
        String uuids = requestKit.getParameter("uuids", "");
        String ifrecommend = requestKit.getParameter("recommend", "0");
        
        String uuid[] = uuids.split(",");
        if(uuid!=null && uuid.length>0){
        	for(int i=0;i<uuid.length;i++){
        		//逐一对审批的信息进行审批状态的改变
        		markethandler.investDelete(uuid[i]);
        	}
        }
    }
    
    /**
     * 根据uuid获取指定投融资项目的详细信息
     * 
     * @param request
     * @param response
     */
    public void investShow(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MarketHandler markethandler = new MarketHandler();
        String uuid = requestKit.getParameter("uuid", "");
        
        Record investInfo = markethandler.getInvestById(uuid);
        request.setAttribute("investInfo", investInfo);
        
        Map fildOptionMap = new HashMap();
        //RecordSet newEnerClass = markethandler.getFildOption("common","NewEnergyClass");
        RecordSet newEnerClass = AchieveHandler.getNewEnergyClassList();
        request.setAttribute("newenergyclass", newEnerClass);
        
        try{
        	this.forwardByStyle("/back/market/investInfo.jsp");
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException ex){
			ex.printStackTrace();
		}
    }
    
    
    /**
     * 修改自荐项目信息
     * 只有在未审核状态、审核不通过状态前台用户才能修改投融资项目信息，审核通过的信息不能修改。
     * 
     * @param request
     * @param response
     */
    public void updateTechneed(HttpServletRequest request,HttpServletResponse response)
    {
    	Connection conn = null;
    	RequestKit requestKit=new RequestKit(request);
    	String forwardurl = "";
    	
    	try {
    		//获取数据库链接
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
    		
    		//从页面上获取技术需求ID
    		String techneedid = requestKit.getParameter("uuid", "");
	    	//从页面上获取技术需求相关内容
	    	Record record = requestKit.getRecordWithPrefix("TECHNEED_");
	    	
	    	//修改投融资项目
	    	record.setEntityName("T_TECHNEED");
	    	record.setConnection(conn);
	    	record.setPrimaryKey("ID");
	    	record.addData("ID", techneedid);
	    	
	    	//数据库中修改投融资项目信息
	    	record.update();
	    	
	    	//添加成功后返回用户中心投融资项目列表页面
	    	//forwardurl = "/market?method=techneedList";
	    	//forward(forwardurl);
	    	
	    	
		} catch(SQLException s){
			s.printStackTrace();
		}finally{
			//释放数据库连接
			if (conn != null)
	        {
				try
	            {
	                conn.close();
	            }catch (SQLException e){
	                e.printStackTrace();
	            }
	        }
		}
    }
    
    
    
    /**
     * 修改自荐项目信息
     * 只有在未审核状态、审核不通过状态前台用户才能修改投融资项目信息，审核通过的信息不能修改。
     * 
     * @param request
     * @param response
     */
    public void updateSelloneself(HttpServletRequest request,HttpServletResponse response)
    {
    	Connection conn = null;
    	RequestKit requestKit=new RequestKit(request);
    	String forwardurl = "";
    	
    	try {
    		//获取数据库链接
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
    		
    		//从页面上获取投融资项目ID
    		String selloneselfid = requestKit.getParameter("uuid", "");
	    	//从页面上获取投融资项目相关内容
	    	Record record = requestKit.getRecordWithPrefix("SELL_");
	    	
	    	//修改投融资项目
	    	record.setEntityName("T_SELLONESELF");
	    	record.setConnection(conn);
	    	record.setPrimaryKey("ID");
	    	record.addData("ID", selloneselfid);
	    	
	    	//数据库中修改投融资项目信息
	    	record.update();
	    	
	    	//添加成功后返回用户中心投融资项目列表页面
	    	//forwardurl = "/market?method=selloneselfList";
	    	//forward(forwardurl);
	    	
		} catch(SQLException s){
			s.printStackTrace();
		}finally{
			//释放数据库连接
			if (conn != null)
	        {
				try
	            {
	                conn.close();
	            }catch (SQLException e){
	                e.printStackTrace();
	            }
	        }
		}
    }
    
    
    /**
     * 修改投融资项目信息
     * 只有在未审核状态、审核不通过状态前台用户才能修改投融资项目信息，审核通过的信息不能修改。
     * 
     * @param request
     * @param response
     */
    public void updateInvest(HttpServletRequest request,HttpServletResponse response)
    {
    	Connection conn = null;
    	RequestKit requestKit=new RequestKit(request);
    	String forwardurl = "";
    	
    	try {
    		//获取数据库链接
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
    		
    		//从页面上获取投融资项目ID
    		String investprojectid = requestKit.getParameter("uuid", "");
	    	//从页面上获取投融资项目相关内容
	    	Record record = requestKit.getRecordWithPrefix("INVEST_");
	    	
	    	//修改投融资项目
	    	record.setEntityName("T_INVESTANDFINANCE");
	    	record.setConnection(conn);
	    	record.setPrimaryKey("ID");
	    	record.addData("ID", investprojectid);
	    	
	    	//数据库中修改投融资项目信息
	    	record.update();
	    	
	    	//添加成功后返回用户中心投融资项目列表页面
	    	//forwardurl = "/market?method=investList";
	    	//forward(forwardurl);
	    	
		} catch(SQLException s){
			s.printStackTrace();
		}finally{
			//释放数据库连接
			if (conn != null)
	        {
				try
	            {
	                conn.close();
	            }catch (SQLException e){
	                e.printStackTrace();
	            }
	        }
		}
    }
}
